home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / SCALE.ZIP / SCALE.CPP
Encoding:
C/C++ Source or Header  |  1996-05-01  |  2.9 KB  |  171 lines

  1. /* Hi guys, here is a bit of code for bitmap scaling. I'm sure that
  2.    it's all been seen before but it may help someone. I know that the
  3.    scale loop can be optimised by introducing more variables, but if
  4.    anyone spots any other optimisations, please point them out.
  5.  
  6.    Compile with bcc, Coments/Suggestions to ks2@st-and.ac.uk
  7.  
  8.    Keith                                */
  9.  
  10.  
  11. //RAW format is first 2 words x,y dimensions,rest data
  12. //ie full screen is 64004 bytes
  13.  
  14. //Use a full screen bm.
  15.  
  16. //#include <defines.h>
  17. //#include <mode13h.h>
  18. #include <conio.h>
  19. #include <dos.h>
  20. //#include <raw.h>
  21. #include <stdlib.h>
  22. //#include <loadpal.h>
  23. #include <string.h>
  24.  
  25. char far* vga=(char far*)MK_FP(0xA000,0);
  26.  
  27. char far* LoadRAW(char* fname,int& x,int& y);
  28.  
  29. void SetMode13h();
  30. void SetModeText();
  31. void FillVGA(char c);
  32.  
  33. //width,height,new width, new height, x,y of top left on screen
  34.  
  35. void ScaleBM(char far* bm,int w,int h,int nw,int nh,int xpos,int ypos)
  36. {
  37.  char far* loc=vga+xpos+320*ypos;
  38.  
  39.  char far* bmc=bm;
  40.  
  41.  static long fpxinc,fpxcur,fpyinc,fpycur;
  42.  
  43.  fpxinc=(long)((long)w*65535L)/(long)nw;
  44.  
  45.  fpyinc=(long)((long)h*65535L)/(long)nh;
  46.  
  47.  register int i,j;
  48.  
  49.  fpycur=0;
  50.  
  51.  for(j=0;j<nh;j++) {
  52.  
  53.     fpxcur=0;
  54.  
  55.     for(i=0;i<nw;i++) {
  56.         loc[i]=bmc[fpxcur>>16];
  57.         fpxcur+=fpxinc; }
  58.     loc+=320;
  59.     bmc=&bm[(fpycur>>16) * w];
  60.     fpycur+=fpyinc; }
  61. }
  62.  
  63. extern void SetMode13h();
  64. extern void SetModeText();
  65.  
  66. main(int argc,char** argv)
  67. {
  68.  char far* bm;
  69.  long x,y,z;
  70.  
  71.  int w,h;
  72.  
  73.  if(argc!=2) exit(1);
  74.  
  75.  bm=LoadRAW(argv[1],w,h);
  76.  
  77.  if(bm==NULL) exit(1);
  78.  
  79. // pal* p;
  80.  
  81.  argv[1][strlen(argv[1])-3]='p';
  82.  argv[1][strlen(argv[1])-2]='a';
  83.  argv[1][strlen(argv[1])-1]='l';
  84.  
  85. // p=LoadPal(argv[1]);
  86.  
  87.  SetMode13h();
  88.  
  89. // SetPal(p);
  90.  
  91.  x=w/2;
  92.  y=h/2;
  93.  
  94.  char q=0;
  95.  
  96.  while(q!=27) {
  97.  
  98.     int xscr,yscr;
  99.  
  100.     for(z=400;z>16;z--) {
  101.  
  102.     xscr=(x<<4)/z;      //d=2^4=16 => screen fill at z=16 with fullscr bm
  103.     yscr=(y<<4)/z;
  104.  
  105.     ScaleBM(bm,w,h,xscr*2,yscr*2,-xscr+160,-yscr+100);
  106.     }
  107.  
  108.     q=getch();
  109.     FillVGA(0); }
  110.  
  111.  SetModeText();
  112.  
  113.  return(0);
  114. }
  115.  
  116. void SetMode13h()
  117. {
  118.  _AH=0;        //BIOS service 00h
  119.  _AL=0x13;    //mode 13h
  120.  asm int 0x10
  121. }
  122.  
  123. void SetModeText()
  124. {
  125.  _AH=0;
  126.  _AL=0x03;
  127.  asm int 0x10
  128. }
  129.  
  130. void FillVGA(char c)
  131. {
  132.  _fmemset(vga,c,64000L);
  133. }
  134.  
  135. #include <stdio.h>
  136. #include <sys\stat.h>
  137. #include <alloc.h>
  138. #include "raw.h"
  139.  
  140. char far* LoadRAW(char* fname,int& x,int& y)
  141. {
  142.  FILE* file=fopen(fname,"rb");
  143.  
  144.  if(file==NULL) {
  145.     printf("Cannot open %s\n",fname);
  146.     return NULL; }
  147.  
  148.  fread(&x,2,1,file);
  149.  fread(&y,2,1,file);
  150.  
  151.  struct stat statbuf;
  152.  
  153.  fstat(fileno(file),&statbuf);
  154.  
  155. // if((long)x*(long)y != (long)statbuf.st_size-4) {
  156. //      printf("Invalid file %s\n",fname);
  157. //      return NULL; }
  158.  
  159.  char far* rawbuf=(char far*)farmalloc((long)statbuf.st_size-4);
  160.  
  161.  if(rawbuf==NULL) {
  162.     printf("Alloc failed\n");
  163.     return NULL; }
  164.  
  165.  for(long i=0;i<statbuf.st_size-4;i++) fread(&rawbuf[i],1,1,file);
  166.  
  167.  fclose(file);
  168.  
  169.  return rawbuf;
  170. }
  171.